#include "ImageLoader.h"

std::map<String, Image*> ImageLoader::images;
StringArray ImageLoader::loadedPaths;


Image * ImageLoader::getImageFromPath(const String& pathToImage){

	if(loadedPaths.contains(pathToImage))
		return images[pathToImage];	

	Image * tmp = ImageFileFormat::loadFrom(File(pathToImage));

	if(!tmp)
		tmp = ImageFileFormat::loadFrom(noimage::file_broken_png, noimage::file_broken_pngSize);

	int w = tmp->getWidth();
	int h = tmp->getHeight();

	Image * img = new Image(Image::ARGB, w, h + 3 + h/2, true);

	/*
	int lineStride = 0, dummy;
	const uint8 * sd = tmp->lockPixelDataReadOnly(0,0, w, h, lineStride, dummy);
	img->setPixelData(0, 0, w, h, sd, lineStride);
	tmp->releasePixelDataReadOnly(sd);
	*/

	for(int x = 0; x < w; x++)
		for(int y = 0; y < h; y++)
			img->setPixelAt(x, y, tmp->getPixelAt(x, y));

	int nh = img->getHeight();
	for(int x = 0; x < w; x++)
		for(int y = 0; y < h / 2; y++)
		{
			img->setPixelAt(x, y + 3 + h, tmp->getPixelAt(x, h - y));
			float alpha = 1.0f - 2.0f * (float) y / (float) h;
			img->multiplyAlphaAt(x, y + 3 + h, alpha);
		}

	delete tmp;
	
	loadedPaths.add(pathToImage);
	images[pathToImage] = img;
	
	return img;
		
}

